<template>
    <div id="content">
        <div>
            <h3>正在进行</h3>
            <ul>
                <li v-show="!item.isFinish" v-for="(item, i) in tasks" :key="i">
                    <div>
                        <input ref="checked" type="checkbox" @change="checkTask(i)">
                        <span>{{item.content}}</span>
                    </div>
                    <span class="del" @click="del(i)">-</span>
                </li>
            </ul>
        </div>
        <div>
            <h3>已经完成</h3>
            <ul>
                <li v-show="item.isFinish" v-for="(item, i) in tasks" :key="i">
                    <div>
                        <input checked type="checkbox" @change="checkTask(i)">
                        <span>{{item.content}}</span>
                    </div>
                    <span class="del"  @click="del(i)">-</span>
                </li>
            </ul>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            // tasks 收集所有的任务
            tasks: []
        }
    },

    created() {
        this.bus.$on('addtask', (task) => {
            this.tasks.push(task);
            console.log(this.tasks)
        })
    },

    methods: {
        checkTask(i) {
            // vue中提供了一个获取dom的方法 就是给元素设置ref属性 
            // 可以通过this.$refs获取该元素
            this.tasks[i].isFinish = !this.tasks[i].isFinish
            var lis = this.$refs.checked;
            for(var i = 0; i<lis.length;i++) {
                lis[i].checked = false
            }
        },
        del(i) {
            this.tasks.splice(i, 1)
        }
    }
}
</script>
<style lang="scss" scoped>
    #content {
        width: 800px;
        margin: 0 auto;
    }
    ul{
        list-style: none;
        margin: 0;
        padding: 0;
    }

    li{
        display: flex;
        height: 40px;
        align-items: center;
        justify-content: space-between;
        position: relative;
        input {
            height: 26px;
            width: 26px;
            vertical-align: middle;
        }
        div span{
            display: inline-block;
            vertical-align: middle;
        }
    }

    .del{
        position: absolute;
        top: 2px;
        right: 5px;
        display: inline-block;
        width: 14px;
        height: 12px;
        border-radius: 14px;
        border: 6px double #FFF;
        background: #CCC;
        line-height: 14px;
        text-align: center;
        color: #FFF;
        font-weight: bold;
        font-size: 14px;
        cursor: pointer;
    }
</style>